package org.goyo.main.web.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.utils.JSONUtil;
import org.goyo.main.domain.entity.system.SysResource;
import org.goyo.main.services.system.ISysResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 系统资源
 * 
 * @author chenchuan
 * 2014年10月23日
 */
@Controller
@RequestMapping("/sysResource")
public class SysResourceController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private ISysResourceService sysResourceService;
	/**
	 * 资源list
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/toSysResourceList")
	public String toSysResourceList(HttpServletRequest request,HttpServletResponse response,Model model){
		List<SysResource> list = null;
		try {
			list = sysResourceService.getAllSysResource();
			model.addAttribute("list", JSONUtil.obj2Json(list));
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return "/system/sysResourceList";
	}
	
	/**
	 * 资源保存
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/saveOrUpdateSysResource")
	@ResponseBody
	public AjaxResult saveOrUpdateSysResource( SysResource sysResource,HttpServletRequest request,HttpServletResponse response){
		
		AjaxResult result = null;
		try {
			String uuid = sysResourceService.saveOrUpdateInfo(sysResource);
			List<SysResource> list = sysResourceService.getAllSysResource();
			result = AjaxResult.resultSuccess("保存成功", list,uuid);
		} catch (Exception e) {
			result = AjaxResult.resultFail("保存失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	/**
	 * 资源删除
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/delSysResource")
	@ResponseBody
	public AjaxResult delSysResource(HttpServletRequest request){
		AjaxResult result = null;
		try {
			String uuid = request.getParameter("uuid");
			sysResourceService.delByUuids(new String[]{uuid});
			List<SysResource> list = sysResourceService.getAllSysResource();
			result = AjaxResult.resultSuccess("删除成功", list);
		} catch (Exception e) {
			result = AjaxResult.resultFail("删除失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
}
